package com.luyuan.crm.sale.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.model.DataModel;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.member.model.Member;
import com.luyuan.crm.sale.model.Order;
import com.luyuan.crm.sale.model.OrderStatus;
import com.luyuan.crm.sale.model.OrderType;
import com.luyuan.crm.sale.service.OrderService;
import com.luyuan.crm.user.Party;
import com.luyuan.crm.user.service.PartyService;
import com.ruijie.components.security.model.AppUser;
import com.ruijie.util.StringUtils;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TableOrder;
import com.ruijie.web.jsf.model.TablePage;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;
import com.ruijie.foundation.query.criteria.RestrictionTemplate;
import com.ruijie.foundation.security.AppUserContext;

@Component("orderSaleManagerAction")
@Scope("view")
public class OrderSaleManagerAction extends BaseAction {

    @Resource(name = "partyService")
    private PartyService partyService;

    @Resource(name = "orderService")
    private OrderService orderService;

    private String uuid;

    private OrderType orderType;

    private Member member = new Member();

    private Party saleman = new Party();

    private OrderStatus status = null;

    private Date startDate;

    private Date endDate;

    public DataModel getOrderTableModel() {
        TablePage tablePage = TableContext.getTablePage();
        List<TableOrder> tableOrders = TableContext.getTableOrders();
        List<org.hibernate.criterion.Order> orders = new ArrayList<org.hibernate.criterion.Order>();
        for (TableOrder tableOrder : tableOrders) {
            org.hibernate.criterion.Order order = tableOrder.toOrder();
            if (order != null) {
                orders.add(order);
            }
        }

        CriteriaTemplate c = CriteriaTemplate.forClass(Order.class);
        RestrictionTemplate r = new RestrictionTemplate(false);
        if (StringUtils.isNotEmpty(uuid)) {
            c.add(r.eq("id", uuid));
        }

        if (member.getName() != null && !member.getName().trim().equals("")) {
            c.createCriteria("customer", "m");
            c.add(r.like("m.name", member.getName()));
        }

        if (startDate != null) {
            c.add(r.gt("createTime", startDate));
        }

        if (endDate != null) {
            c.add(r.lt("createTime", endDate));
        }

        AppUser appUser = (AppUser) AppUserContext.getCurrentUser();
        Party party = partyService.getByAppUser(appUser);
        c.createCriteria("saleman", "s").createCriteria("s.partyGroup", "g");
        c.add(r.or(r.eq("s.partyGroup", party.getPartyGroup()), r.eq("g.parentGroup", party.getPartyGroup())));

        if (saleman.getUserName() != null && !saleman.getUserName().trim().equals("")) {
            c.add(r.like("s.userName", saleman.getUserName()));
        }

        if (saleman.getPartyGroup() != null) {
            c.createCriteria("s.partyGroup", "p");
            c.add(r.eq("p.id", saleman.getPartyGroup().getId()));
        }

        if (status != null) {
            c.add(r.eq("orderStatus", status));
        }

        c.addOrder(org.hibernate.criterion.Order.desc("createTime"));
        Page page = orderService.pagedQuery(c, tablePage.getPage(), tablePage.getPageSize(), orders);
        return new PageDataModel(page);
    }

    public String getOrderType(int value) {
        return OrderType.valueOf(value).getName();
    }

    public OrderType getOrderType() {
        return orderType;
    }

    public void setOrderType(OrderType orderType) {
        this.orderType = orderType;
    }

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    public Party getSaleman() {
        return saleman;
    }

    public void setSaleman(Party saleman) {
        this.saleman = saleman;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public OrderStatus getStatus() {
        return status;
    }

    public void setStatus(OrderStatus status) {
        this.status = status;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }
}
